package com.pccw.service.hessianservice;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.remoting.caucho.HessianExporter;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.util.NestedServletException;

import com.caucho.services.server.ServiceContext;

public class MyHessianServiceExport extends HessianExporter implements HttpRequestHandler {

	/**
	 * Processes the incoming Hessian request and creates a Hessian response.
	 */
	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (!"POST".equals(request.getMethod())) {
			throw new HttpRequestMethodNotSupportedException(request.getMethod(),new String[] {"POST"}, "HessianServiceExporter only supports POST requests");
		}
		
		if (!"null".equals(request.getMethod())) {
			//throw new HttpRequestMethodNotSupportedException(request.getMethod(),new String[] {"POST"}, "You Must Login");
		}
		response.setContentType(CONTENT_TYPE_HESSIAN);
		System.out.println("-------");
	    String serviceId = request.getPathInfo();
	    String objectId = request.getParameter("id");
	    if (objectId == null){
		   objectId = request.getParameter("ejbid");
	    }
	    ServiceContext.begin(request, serviceId, objectId);
		try {
			invoke(request.getInputStream(), response.getOutputStream());
		}catch (Throwable ex) {
		  throw new NestedServletException("Hessian skeleton invocation failed", ex);
		}finally{
			ServiceContext.end();
		}
	}

}
